<!-- 网站信息 -->
<template>
    <div class="website-information">
        <el-form ref="formRef" :rules="rules" class="ls-form" :model="formData" label-width="120px">
            <el-card shadow="never" class="!border-none">
                <div class="text-xl font-medium mb-[20px]">{{ $t('后台设置') }}</div>
                <el-form-item :label="$t('网站名称')" prop="name">
                    <div class="w-80">
                        <el-input v-model.trim="formData.name" :placeholder="$t('请输入网站名称')" maxlength="30"
                            show-word-limit />
                    </div>
                </el-form-item>
                <el-form-item :label="$t('网站图标')" prop="web_favicon" required>
                    <div>
                        <material-picker v-model="formData.web_favicon" :limit="1" />
                        <div class="form-tips">{{ $t('建议尺寸:100*100像素,支持jpg,jpeg,png格式') }}</div>
                    </div>
                </el-form-item>
                <el-form-item :label="$t('网站LOGO')" prop="web_logo" required>
                    <div>
                        <material-picker v-model.trim="formData.web_logo" :limit="1" />
                        <div class="form-tips">{{ $t('建议尺寸:100*100像素,支持jpg,jpeg,png格式') }}</div>
                    </div>
                </el-form-item>
                <el-form-item :label="$t('登录页广告图')" prop="login_image" required>
                    <div>
                        <material-picker v-model.trim="formData.login_image" :limit="1" />
                        <div class="form-tips">{{ $t('建议尺寸:100*100像素,支持jpg,jpeg,png格式') }}</div>
                    </div>
                </el-form-item>
            </el-card>
            <el-card shadow="never" class="!border-none mt-4">
                <div class="text-xl font-medium mb-[20px]">{{ $t('移动端设置') }}</div>
                <el-form-item :label="$t('前台名称')" prop="h5_title">
                    <div class="w-80">
                        <el-input v-model.trim="formData.h5_title" :placeholder="$t('请输入前台名称')" maxlength="30"
                            show-word-limit></el-input>
                    </div>
                </el-form-item>
                <el-form-item :label="$t('前台LOGO')" prop="h5_logo">
                    <div>
                        <material-picker v-model="formData.h5_logo" :limit="1" />
                        <div class="form-tips">{{ $t('建议尺寸:100*100px,支持jpg,jpeg,png格式') }}</div>
                    </div>
                </el-form-item>
            </el-card>
            <el-card shadow="never" class="!border-none mt-4">
                <div class="text-xl font-medium mb-[20px]">{{ $t('PC端设置') }}</div>
                <el-form-item :label="$t('PC端LOGO')" prop="pc_logo">
                    <div>
                        <material-picker v-model="formData.pc_logo" :limit="1" />
                        <div class="form-tips">{{ $t('建议尺寸:120*28px,支持jpg,jpeg,png格式') }}</div>
                    </div>
                </el-form-item>
                <el-form-item :label="$t('网站标题')" prop="pc_title">
                    <div class="w-80">
                        <el-input v-model.trim="formData.pc_title" :placeholder="$t('请输入PC端网站标题')" maxlength="30"
                            show-word-limit />
                    </div>
                </el-form-item>
                <el-form-item :label="$t('网站图标')" prop="pc_ico">
                    <div>
                        <material-picker v-model="formData.pc_ico" :limit="1" />
                        <div class="form-tips">{{ $t('建议尺寸:100*100像素,支持jpg,jpeg,png格式') }}</div>
                    </div>
                </el-form-item>
                <el-form-item :label="$t('网站描述')" prop="pc_desc">
                    <div class="w-80">
                        <el-input v-model.trim="formData.pc_desc" :placeholder="$t('请输入PC端网站描述')" />
                    </div>
                </el-form-item>
                <el-form-item :label="$t('网站关键词')" prop="pc_keywords">
                    <div class="w-80">
                        <el-input v-model.trim="formData.pc_keywords" :placeholder="$t('请输入PC端网站关键词')" />
                    </div>
                </el-form-item>
            </el-card>
        </el-form>
        <footer-btns v-perms="['setting/web.web_setting/setWebsite']">
            <el-button type="primary" @click="handleSubmit">{{ $t('保存') }}</el-button>
        </footer-btns>
    </div>
</template>

<script lang="ts" setup name="webInformation">
import { useI18n } from 'vue-i18n'
const { t } = useI18n()
import { getWebsite, setWebsite } from '@/api/setting/website'
import useAppStore from '@/stores/modules/app'
import type { FormInstance } from 'element-plus'
const formRef = ref<FormInstance>()

const appStore = useAppStore()
// 表单数据
const formData = reactive({
    name: '', // 网站名称
    web_favicon: '', // 网站图标
    web_logo: '', // 网站logo
    login_image: '', // 登录页广告图
    h5_title: '',
    h5_logo: '',
    pc_logo: '',
    pc_title: '',
    pc_desc: '',
    pc_ico: '',
    pc_keywords: ''
})

// 表单验证
const rules = {
    name: [
        {
            required: true,
            message: t('请输入网站名称'),
            trigger: ['blur']
        }
    ],
    web_favicon: [
        {
            required: true,
            message: t('请选择网站图标'),
            trigger: ['change']
        }
    ],
    web_logo: [
        {
            required: true,
            message: t('请选择网站logo'),
            trigger: ['change']
        }
    ],
    login_image: [
        {
            required: true,
            message: t('请选择登录页广告图'),
            trigger: ['change']
        }
    ],
    h5_title: [
        {
            required: true,
            message: t('请输入移动端名称'),
            trigger: ['blur']
        }
    ],
    h5_logo: [
        {
            required: true,
            message: t('请选择移动端LOGO'),
            trigger: ['change']
        }
    ],
    pc_logo: [
        {
            required: true,
            message: t('请选择PC端LOGO'),
            trigger: ['change']
        }
    ],
    pc_title: [
        {
            required: true,
            message: t('请输入PC端网站标题'),
            trigger: ['blur']
        }
    ],
    pc_ico: [
        {
            required: true,
            message: t('请选择PC端网站图标'),
            trigger: ['change']
        }
    ]
}

// 获取备案信息
const getData = async () => {
    const data = await getWebsite()
    for (const key in formData) {
        //@ts-ignore
        formData[key] = data[key]
    }
}

// 设置备案信息
const handleSubmit = async () => {
    await formRef.value?.validate()
    await setWebsite(formData)
    appStore.getConfig()
    getData()
}

getData()
</script>

<style lang="scss" scoped></style>
